<template>
  <div class="app-container">
    <router-link to="article/create">
      <el-button type="primary" size="small"><i class="el-icon-plus" /> 新增文章</el-button>
    </router-link>

    <el-card class="table_style">
      <el-table :data="articles" border>
        <el-table-column label="ID" prop="id" width="80" />
        <el-table-column label="标题" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属品牌">
          <template slot-scope="scope">
            <el-tag type="primary">{{ scope.row.brand.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="image" label="缩略图">
          <template slot-scope="scope">
            <img :src="scope.row.image" :alt="scope.row.title" class="pic-style">
          </template>
        </el-table-column>
        <el-table-column prop="author" label="作者">
          <template slot-scope="scope">
            <span>{{ scope.row.author }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="views" label="阅读数">
          <template slot-scope="scope">
            <span>{{ scope.row.views }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdAt|order_format }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import '../../filter'
import { fetchArticleList, deleteArticle } from '@/api/article'

export default {
  data() {
    return {
      articles: []
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      const res = await fetchArticleList()
      this.articles = res.data.articles
    },
    // 点击跳转到编辑页面
    handleEdit(row) {
      this.$router.push({
        name: 'ArticleEdit',
        params: { id: row.id }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认要删除该文章吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        const res = await deleteArticle(row.id)
        this.init()
        this.$message({
          type: 'success',
          message: res.message
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
.table_style {
  margin-top: 15px;
}
.pic-style{
  width: 80px;
  height: 60px;
}
</style>
